//
//  JiFenOrderDetailViewController.m
//  JiuJiuShangCheng
//
//  Created by jiujiukeji on 2018/7/17.
//  Copyright © 2018年 jiujiukeji. All rights reserved.
//

#import "JiFenOrderDetailViewController.h"
#import "OrderTableViewCell.h"

@interface JiFenOrderDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSDictionary *orderDic;
@end

@implementation JiFenOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = tableViewBgColor;
    [self loadBaseTitleLable:@"积分订单详情"];
    
    [self.view addSubview:self.tableView];
    [self getOrderDetail];
}
- (void)getOrderDetail
{
    if (self.ordersNumber) {
        NSString *code = [NSString stringWithFormat:@"%@%@",safeCode,[GetTimestamp getNowTimeTimestamp]];
        [MBProgressHUD showMessage:nil toView:self.view];
        [TFNetworking getWithURLString:[NSString stringWithFormat:@"%@/api/json/member/order.ashx?action=integralordercontent&companyid=%@&code=%@&timestamp=%@&ordersNumber=%@&from=mobile",kServerURL,companyId,[MD5Encrypt  MD5ForLower32Bate:code],[GetTimestamp getNowTimeTimestamp],self.ordersNumber] parameters:nil success:^(id responseObject) {
            [MBProgressHUD hideHUDForView:self.view];
            self.orderDic = [NSDictionary dictionary];
            if ([responseObject[@"status"] intValue] == 1) {
                self.orderDic = responseObject;
            }
            [self.tableView reloadData];
            
        } failure:^(NSError *error) {
            [TFHUD addRemindHUD:self.view message:@"网络不给力..."];
        }];
    }
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        if (_orderDic) {
            NSArray *array = _orderDic[@"OrderGoodsList"];
            return array.count;
        } else {
            return 1;
        }
        
    } else if (section == 1) {
        return 4;
    } else {
        return 3;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        OrderTableViewCell *cell = [[OrderTableViewCell alloc] init];
        cell.contentView.backgroundColor = UIColorWhite;
        cell.priceLB.text = @"应付积分：";
        // 赋值
        if (self.orderDic) {
            NSArray *array = _orderDic[@"OrderGoodsList"];
            NSDictionary *dic = array[indexPath.row];
            [cell.goodsImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http:%@",dic[@"CartImg"]]]];
            cell.goodsTitleLB.text = dic[@"CommodityName"];
            cell.priceLB.text = [NSString stringWithFormat:@"应付积分：%@",dic[@"CommodityXPrice"]];
            cell.describeLB.text = dic[@"CommodityProperty"];
            cell.numLB.text = [NSString stringWithFormat:@"×%@",dic[@"CommodityNumber"]];
            
        }
        cell.selectionStyle = NO;
        return cell;
    } else {
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        UILabel *label = [UILabel labelWithText:@"" textColor:UIColorFromRGB(0x333333) fontSize:14];
        [label sizeToFit];
        [cell.contentView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell.contentView).offset(rateWidth(30));
            make.centerY.equalTo(cell.contentView);
        }];
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = globalGray;
        [cell.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(cell.contentView);
            make.height.equalTo(@(1));
        }];
        
        if (indexPath.section == 1) {
            
            NSArray *array = @[[NSString stringWithFormat:@"地址：%@",self.orderDic[@"ReceiptAddress"]],[NSString stringWithFormat:@"邮编：%@",self.orderDic[@"ReciptZipCode"]],[NSString stringWithFormat:@"姓名：%@",self.orderDic[@"ReceiptName"]],[NSString stringWithFormat:@"电话：%@",self.orderDic[@"ReceiptPhone"]]];
            label.text = array[indexPath.row];
        } else {
            NSArray *array1 = [self.orderDic[@"orderDeliveryInfo"] componentsSeparatedByString:@","];
            NSArray *array = @[[NSString stringWithFormat:@"订单日期：%@",self.orderDic[@"orderDate"]],[NSString stringWithFormat:@"快递公司：%@",array1[0]],[NSString stringWithFormat:@"快递单号：%@",array1[1]]];
            label.text = array[indexPath.row];
        }
        
        cell.selectionStyle = NO;
        return cell;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return rateHeight(200);
    } else {
        return rateHeight(90);
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return rateHeight(90);
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if ([self.orderDic[@"orderStatus"] intValue] == 3) {
        // 待收货
        if (section == 2) {
            return rateHeight(130);
        } else {
            return 0.01;
        }
        
    } else {
        return 0.01;
    }
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *bgV = [[UIView alloc] init];
    bgV.backgroundColor = tableViewBgColor;
    NSArray *array = @[[NSString stringWithFormat:@"订单号：%@",self.ordersNumber],@"收货地址",@"订单详情"];
    UILabel *label = [UILabel labelWithText:array[section] textColor:UIColorFromRGB(0x323232) fontSize:15];
    [label sizeToFit];
    [bgV addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgV).offset(rateWidth(30));
        make.centerY.equalTo(bgV);
    }];
    if (section == 0) {
        UILabel *label = [UILabel labelWithText:self.orderDic[@"orderStatusName"] textColor:UIColorFromRGB(0x323232) fontSize:15];
        [label sizeToFit];
        [bgV addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(bgV).offset(-rateWidth(20));
            make.centerY.equalTo(bgV);
        }];
    }
    return bgV;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if ([self.orderDic[@"orderStatus"] intValue] == 3) {
        // 待收货
        if (section == 2) {
            UIView *bgV = [[UIView alloc] init];
            bgV.backgroundColor = kClearColor;
            
            UIButton *btn = [UIButton buttonWithTitle:@"确认收货" fontSize:15 titleColor:UIColorWhite background:navBarColor cornerRadius:5];
            [bgV addSubview:btn];
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(bgV);
                make.size.mas_offset(CGSizeMake(rateWidth(600), rateHeight(80)));
            }];
            [btn addTarget:self action:@selector(shouHuoAction) forControlEvents:(UIControlEventTouchUpInside)];
            
            return bgV;
        } else {
            return nil;
        }
        
    } else {
        return nil;
    }
}
#pragma mark - 确认收货
- (void)shouHuoAction
{
    WeakSelf
    UIAlertController *alertController = [UIAlertController initWithMessage:@"是否确认收货？" bolck:^(void) {
        NSString *code = [NSString stringWithFormat:@"%@%@",safeCode,[GetTimestamp getNowTimeTimestamp]];
        [MBProgressHUD showMessage:nil toView:weakSelf.view];
        [TFNetworking getWithURLString:[NSString stringWithFormat:@"%@/api/json/member/order.ashx?action=integralordershouhuo&companyid=%@&code=%@&timestamp=%@&ordersNumber=%@",kServerURL,companyId,[MD5Encrypt  MD5ForLower32Bate:code],[GetTimestamp getNowTimeTimestamp],self.orderDic[@"ordersNumber"]] parameters:nil success:^(id responseObject) {
            [MBProgressHUD hideHUDForView:weakSelf.view];
            if ([responseObject[@"status"] intValue] == 1) {
                // 发送通知
                [[NSNotificationCenter defaultCenter] postNotificationName:@"confirmJiFenOrder" object:@"" userInfo:nil];
                [self.navigationController popViewControllerAnimated:YES];
            }
            
        } failure:^(NSError *error) {
            [TFHUD addRemindHUD:weakSelf.view message:@"网络不给力..."];
        }];
    } cancelBolck:^(void) {
        
    }];
    [self presentViewController:alertController animated:YES completion:nil];
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, kScreenWidth, kScreenHeight-SafeAreaTopHeight) style:(UITableViewStyleGrouped)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
